import Cookies from 'js-cookie';
import { login, logout, agentInfo } from '@/api/login'
import { Message } from 'iview'
import { setToken, setId, setUser, setAccess, getToken, getUserId, getUser, getName, removeToken } from '@/utils/auth'

const user = {
    state: {
      token: getToken(),
      user: getUser(),
      user_id: getUserId(),
      avatar: localStorage.avatar
    },
    mutations: {
        SET_ID: (state, id) => {
            state.user_id = id
        },
        SET_TOKEN: (state, token) => {
            state.token = token
        },
        SET_NAME: (state, name) => {
            state.name = name
        },
        SET_AVATAR: (state, avatar) => {
          state.avatar = avatar
        }
    },
    actions: {
        loging({ commit, state }, user) {
            const username = user.userName.trim();
            return new Promise((resolve, reject) => {
                login(username, user.password).then(response => {
                    if (response.data.errorCode === 0) {
                        commit('SET_TOKEN', response.data.data.token);
                        commit('SET_ID', response.data.data.user_num);
                        commit('SET_NAME', response.data.data.merchant.short_name);
                        commit('SET_AVATAR', response.data.data.avatar);
                        localStorage.avatar = response.data.data.avatar;
                        setToken(response.data.data.token);
                        setId(response.data.data.user_num);
                        setUser(response.data.data.login_username); // 返回用户信息
                        setAccess(0);
                        Message.success(response.data.errorMsg);
                        resolve();
                    } else {
                        Message.error(response.data.errorMsg);
                        reject();
                    }
                }).catch(error => {
                    reject(error);
                });
            });
        },
        loginout({ commit }) {
            return new Promise((resolve, reject) => {
                logout().then(response => {
                    if (response.data.errorCode === 0) {
                        commit('SET_TOKEN', '');
                        Cookies.remove('merchant_id');
                        Cookies.remove('merchant_token');
                        Cookies.remove('merchant_access');
                        if (!Cookies.get('merchant_password')) {
                            Cookies.remove('merchant_user');
                        }
                        localStorage.clear();
                        Message.success(response.data.errorMsg)
                        resolve()
                    } else {
                        Message.error(response.data.errorMsg)
                        reject()
                    }
                }).catch(error => {
                    reject(error)
                })
            })
        },
        // 前端 登出
        FedLogOut({ commit }) {
            return new Promise(resolve => {
                commit('SET_TOKEN', '')
                removeToken()
                resolve()
            })
        },
        GetAgentInfo({ commit }, id) {
          return new Promise((resolve, reject) => {
            agentInfo(id).then(response => {
              resolve(response)
            })
          })
        }
    }
};

export default user;
